<template>
  <div v-if="showDialog">
    <!-- <el-button type="primary" @click="centerDialogVisible = true">便捷登录</el-button> -->

    <el-dialog
      title="亲，请先登录，然后提交订单哦"
      :visible.sync="centerDialogVisible"
      center
    >
      <el-form :model="form" ref="loginForm">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input
            type="text"
            autocomplete="off"
            placeholder="用户名/手机"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input
            placeholder="密码"
            type="password"
            v-model="form.password"
          ></el-input>
          <p class="form-text">
            <nuxt-link to="#">忘记密码</nuxt-link>
          </p>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleLoginSubmit">登录</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ["showDialog"],
  data() {
    return {
      dialogTableVisible: false,
      centerDialogVisible: true,
      form: {
        username: "",
        password: ""
      },
      formLabelWidth: "100px"
    };
  },
  methods: {
    handleLoginSubmit() {
      // 发送请求之前先验证form表单
      this.$refs.loginForm.validate((isValid, inValidField) => {
        //如果通过验证
        if (isValid) {
          this.$store.dispatch("user/login", this.form).then(res => {
            // 成功提示
            if (res.data.token) {
              this.$message({
                message: "登录成功",
                type: "success"
              });
              //隐藏弹窗
              this.centerDialogVisible = false;
            }
          });
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
/deep/ .el-dialog {
  width: 40%;
}
/deep/ .el-input {
  width: 80% !important;
}
</style>
